package cn.rongdao.jrkabao.ui.remind.uicomponent;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.switchbutton.CustomSwitch;

import cn.rongdao.jrkabao.R;

/**
 * Created by jack on 2017/11/23
 */

public class EditCreditCardUIComponent extends RelativeLayout {

    public EditCreditCardUIComponent(Context context) {
        super(context);
    }

    public EditCreditCardUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditCreditCardUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvName;
    private TextView tvContent;
    private EditText etInput;
    private ImageView ivArrow;
    private CustomSwitch customSwitch;
    private View viewLine;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvName = (TextView) findViewById(R.id.lecc_tv_name);
        tvContent = (TextView) findViewById(R.id.lecc_tv_content);
        etInput = (EditText) findViewById(R.id.lecc_et_input);
        ivArrow = (ImageView) findViewById(R.id.lecc_iv_arrow);
        customSwitch = (CustomSwitch) findViewById(R.id.lecc_switch);
        viewLine = findViewById(R.id.lecc_view_line);
    }

    public void setName(String name) {
        tvName.setText(FRString.valueOf(name));
    }

    public void setBlackContent(String content) {
        tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.c333333));
        tvContent.setText(FRString.valueOf(content));
    }

    public void setGrayContentText(String content) {
        tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.c999999));
        tvContent.setText(FRString.valueOf(content));
    }

    public void setSwitchVisible() {
        ivArrow.setVisibility(GONE);
        customSwitch.setVisibility(VISIBLE);
        etInput.setVisibility(View.GONE);
        setViewLineGone();
    }

    public void setViewLineGone() {
        viewLine.setVisibility(View.GONE);
    }

    public void setRightGone() {
        ivArrow.setVisibility(GONE);
        customSwitch.setVisibility(VISIBLE);
    }

    public void setMiddleGone() {
        tvContent.setVisibility(View.GONE);
        etInput.setVisibility(View.GONE);
    }

    public String getInputContent() {
        return etInput.getText().toString().trim();
    }

    public void setInputContent(String content) {
        etInput.setVisibility(VISIBLE);
        tvContent.setVisibility(GONE);
        ivArrow.setVisibility(GONE);
        etInput.setText(content);
    }

    public void setInputDisable() {
        etInput.setEnabled(false);
    }

    public void setInputHint(String hint) {
        etInput.setVisibility(VISIBLE);
        tvContent.setVisibility(GONE);
        ivArrow.setVisibility(GONE);
        etInput.setHint(FRString.valueOf(hint));
    }

    public void setNumberStyle() {
        etInput.setInputType(InputType.TYPE_CLASS_NUMBER);
    }

    public void setNumberInputMaxLength(int maxLength) {
        etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }

    public void setCustomSwitchListener(CustomSwitch.OnCheckedChangeListener customSwitchListener) {
        customSwitch.setOnCheckedChangeListener(customSwitchListener);
    }

    public void setCustomSwitchState(boolean isChecked) {
        customSwitch.setState(isChecked);
    }

    public void setInputMoneyStyle(){
        etInput.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        etInput.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }
}